package com.bbx.taxi.client.Activity.Guide;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

import com.bbx.taxi.official.R;
import com.bbx.taxi.client.Bean.Extra.GuideMsg;
import com.bbx.taxi.client.Bean.Extra.SetMsg;
import com.bbx.taxi.client.widget.StatusBar.StatusBar;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

/**
 * 
 * @{# GuideActivity.java Create on 2013-5-2 下午10:59:08
 * 
 *     class desc: 引导界面
 * 
 *     <p>
 *     Copyright: Copyright(c) 2013
 *     </p>
 * @Version 1.0
 * @Author <a href="mailto:gaolei_xj@163.com">Leo</a>
 * 
 * 
 */
public class GuideActivity extends Activity implements OnPageChangeListener {

	@InjectView(R.id.viewpager)ViewPager viewPager;
	@InjectView(R.id.dots_top)LinearLayout dots_top;
	ViewPagerAdapter vpAdapter;
	private List<View> views;

	// 底部小点图片
	private ImageView[] dots;

	// 记录当前选中位置
	private int currentIndex;
	
    ImageHandler handler = new ImageHandler(new WeakReference<GuideActivity>(this));  
    private int intenttype;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_guide);
		//透明状态栏  
		getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);  
		//透明导航栏  
		getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
		StatusBar.setTranslucentStatus(this, true);
		ButterKnife.inject(this);
		Intent intent = getIntent();
		intenttype = intent.getIntExtra(GuideMsg.extra_guide, GuideMsg.guide_welcome);
		// 初始化页面
		initViews();
	}

	private void initViews() {
		LayoutInflater inflater = LayoutInflater.from(this);

		views = new ArrayList<View>();
		// 初始化引导图片列表
		views.add(inflater.inflate(R.layout.guide_new_one, null));
		views.add(inflater.inflate(R.layout.guide_new_two, null));
//		views.add(inflater.inflate(R.layout.guide_new_three, null));
//		views.add(inflater.inflate(R.layout.guide_new_four, null));
		views.add(inflater.inflate(R.layout.guide_new_five, null));

		// 初始化Adapter
		vpAdapter = new ViewPagerAdapter(views, this,intenttype);
		viewPager.setAdapter(vpAdapter);
		ViewPagerScroller scroller = new ViewPagerScroller(this);
		scroller.setScrollDuration(800);
		scroller.initViewPagerScroll(viewPager);//这个是设置切换过渡时间为0.8秒
		// 绑定回调
		viewPager.setOnPageChangeListener(this);
		setOnPlayTime(3000);
		setOnIsCirculate(false);
		// 初始化底部小点
		initDots(views.size());
		 //开始轮播效果  
        handler.sendEmptyMessageDelayed(ImageHandler.MSG_UPDATE_IMAGE, ImageHandler.time);  
        
	}

	private void initDots(int count) {
		int chat_dot_margin_lr = (int) getResources().getDimension(
				R.dimen.chat_dot_margin_lr);
		int chat_dot_wh_big = (int) getResources().getDimension(
				R.dimen.chat_dot_wh_big);
		for (int i = 0; i < count; i++) {
			android.widget.LinearLayout.LayoutParams lp = new android.widget.LinearLayout.LayoutParams(
					chat_dot_wh_big, chat_dot_wh_big);
			lp.leftMargin = chat_dot_margin_lr;
			lp.rightMargin = chat_dot_margin_lr;
			dots_top.addView(dotsItem(i), lp);
		}

		currentIndex = 0;
		dots_top.getChildAt(currentIndex).setSelected(true);
	}
	/**
	 * ，底部小圆点
	 * 
	 * @param position
	 * @return
	 */
	private ImageView dotsItem(int position) {
		LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
		View layout = inflater.inflate(R.layout.dot_image2, null);
		ImageView iv = (ImageView) layout.findViewById(R.id.face_dot);
		iv.setId(position);
		return iv;
	}
	private void setCurrentDot(int position) {		
		for (int i = 0; i < dots_top.getChildCount(); i++) {
			dots_top.getChildAt(i).setSelected(false);
		}
		dots_top.getChildAt(position).setSelected(true);

		currentIndex = position;
	}

	// 当滑动状态改变时调用
	@Override
	public void onPageScrollStateChanged(int arg0) {
		 switch (arg0) {  
         case ViewPager.SCROLL_STATE_DRAGGING:  
             handler.sendEmptyMessage(ImageHandler.MSG_KEEP_SILENT);  
             break;  
         case ViewPager.SCROLL_STATE_IDLE:  
             handler.sendEmptyMessageDelayed(ImageHandler.MSG_UPDATE_IMAGE, ImageHandler.time);  
             break;  
         default:  
             break;  
         }  
	}

	// 当当前页面被滑动时调用
	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
	}

	// 当新的页面被选中时调用
	@Override
	public void onPageSelected(int arg0) {
		// 设置底部小点选中状态
		setCurrentDot(arg0);
		handler.sendMessage(Message.obtain(handler, ImageHandler.MSG_PAGE_CHANGED, arg0, 0));  
	}
    /**
     * 设置是否循环
     */
    public void setOnIsCirculate(boolean isCirculate){
    	handler.isCirculate=isCirculate;
    }
    /**
     * 设置是轮播时间
     */
    public void setOnPlayTime(long time){
    	handler.time=time;
    }
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if (keyCode == KeyEvent.KEYCODE_BACK) {

			return true;

		}
		return false;
	}
}
